package com.ok.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ok.common.domain.po.Result;
import com.ok.community.domain.dto.OkCardDTO;
import com.ok.community.domain.po.OkCard;
import com.ok.community.service.IOkCardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.reflections.util.Utils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 帖子 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-04-29
 */
@Api(tags = "帖子相关接口")
@RestController
@RequestMapping("/card")
@RequiredArgsConstructor
public class OkCardController {

    private final IOkCardService okCardService;

    @ApiOperation(value = "删除帖子")
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Long id){
        okCardService.delete(id);
        return Result.success("删除成功");
    }

    @ApiOperation(value = "查询帖子")
    @GetMapping("/page")
    public Result page(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                       @RequestParam(required = false) Integer categoryId){
        Page<OkCard> page = new Page<>(pageNo,pageSize);
        LambdaQueryWrapper<OkCard> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryId != null,OkCard::getCategoryId,categoryId);
        okCardService.page(page, queryWrapper);
        return Result.success(page);
    }

    @ApiOperation("*添加帖子")
    @PostMapping("/add")
    public Result add(@RequestBody OkCardDTO cardDTO){

        okCardService.add(cardDTO);
        return Result.success("帖子添加成功");
    }

}
